<?php
namespace app\mec\controller;
use think\Request;
use think\Db;
class Information extends Base {

    /***
     * @return mixed|\think\response\Json
     * 首页
     */
    public function index(){
        if(request()->isAjax()){
            $map['a.mechanism_id'] = session('mmid');
            $page = input('page') ? input('page'):1;
            $limit = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::name('information')->alias('a')
                ->join('up_information_type uit','uit.information_type_id = a.information_type_id','left')
                ->where($map)
                ->count();//计算总页面
            $list = Db::name('information')->alias('a')
                ->join('up_information_type uit','uit.information_type_id = a.information_type_id','left')
                ->join('up_mechanism um','um.mechanism_id = a.mechanism_id','left')
                ->field('a.*,uit.information_type_name,um.mechanism_name')
                ->order('a.information_status desc,information_release_time desc')
                ->where($map)
                ->page($page, $limit)
                ->select();//计算总页面
            foreach ($list as $key=>&$vo){
                if($vo['admin_id'] == session('mid')){
                    $vo['my_update'] = 1;//可以编辑
                }else{
                    $vo['my_update'] =2;//不可以编辑

                }
            }
            return json(['code'=>0,'msg'=>'','count'=>$count,'data'=>$list]);
        }
        return $this->fetch('information/index');
    }

    /***
     * 发布资讯
     */
    public function add(){
        if(request()->isAjax()){
            $data['information_img'] = input('param.images');
            $data['information_title'] = input('param.information_title');
            $data['information_introduce'] = input('param.information_introduce');
            $data['information_release_time'] = date('Y-m-d H:i:s',time());
            $data['mechanism_for'] = input('param.mechanism_for');
            $data['information_type_id'] = input('param.information_type_id');
            $data['information_content'] = input('param.information_content');
            $data['information_status'] = 3;
            $data['mechanism_id'] = session('mmid');
            $data['admin_id'] = session('mid');
            $list = Db::table('up_information')->insertGetId($data);
            if($list){
                return json(['code' => 200, 'data' =>"", 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'data' => '', 'msg' => '添加失败']);
            }
        }
        $res = Db::table('up_information_type')->select();
        $this->assign('list',$res);
        return $this->fetch();
    }
    /***
     * 审核
     */
//    public function examine(){
//        if(request()->isAjax()){
//            $id = input('param.id');
//            $status = input('param.radio1');
//            if($status==2){
//                $data['mechanism_sh_reason'] = input('param.content');
//            }
//            $data['information_status'] = $status;
//            $whereMap['information_id'] = $id;
//            $list = Db::table('up_information')->where($whereMap)->update($data);
//            if($list){
//                return json(['code' => 200, 'data' =>"", 'msg' => '审核成功']);
//            }else{
//                return json(['code' => 100, 'data' => '', 'msg' => '审核失败']);
//            }
//        }
//        $id = input('param.id');
//        $list = Db::name('information')->alias('a')
//            ->join('up_information_type uit','uit.information_type_id = a.information_type_id','left')
//            ->join('up_mechanism um','um.mechanism_id = a.mechanism_id','left')
//            ->field('a.*,uit.information_type_name,um.mechanism_name')
//            ->where('information_id',$id)
//            ->find();//计算总页面
//        $this->assign('res',$list);
//        return $this->fetch();
//    }

    /**
     * 编辑
     */
    public function update(){
        if(request()->isAjax()){
            $where['information_id'] = input('param.id');
            $data['information_img'] = input('param.images');
            $data['information_title'] = input('param.information_title');
            $data['information_introduce'] = input('param.information_introduce');
            $data['information_release_time'] = input('param.information_release_time');
            $data['mechanism_for'] = input('param.mechanism_for');
            $data['information_type_id'] = input('param.information_type_id');
            $data['information_status'] = 3;
            $res = Db::table('up_information')->where($where)->update($data);
            if($res){
                return json(['code' => 200, 'data' =>"", 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'data' => '', 'msg' => '编辑失败']);
            }
        }
        $id = input('param.id');
        $list = Db::name('information')->alias('a')
            ->join('up_information_type uit','uit.information_type_id = a.information_type_id','left')
            ->join('up_mechanism um','um.mechanism_id = a.mechanism_id','left')
            ->field('a.*,uit.information_type_name,um.mechanism_name')
            ->where('information_id',$id)
            ->find();//计算总页面
        $res = Db::table('up_information_type')->select();
        $this->assign('res',$list);
        $this->assign('list',$res);
        return $this->fetch();

    }

    /***
     * @return mixed
     * 查看详情
     */
    public function detail(){
        $id = input('param.id');
        $list = Db::name('information')->alias('a')
            ->join('up_information_type uit','uit.information_type_id = a.information_type_id','left')
            ->join('up_mechanism um','um.mechanism_id = a.mechanism_id','left')
            ->field('a.*,uit.information_type_name,um.mechanism_name')
            ->where('a.information_id',$id)
            ->find();//计算总页面
        $this->assign('res',$list);
        return $this->fetch();
    }
}